package com.taxsystem.chengwenzhi.branch_map;

import java.io.InputStream;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfigeration;
import com.baidu.mapapi.map.MyLocationConfigeration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.main.taxsystem.R;
import com.taxsystem.tool.GetRequestTask;

public class MapDetailActivity extends Activity {

	private static final String TAG = "MapDetailActivity";
	private MapView mMapView = null;
	private BaiduMap mBaiduMap = null;
	private PoiSearch mPoiSearch = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		// 在使用SDK各组件之前初始化context信息，传入ApplicationContext
		// 注意该方法要再setContentView方法之前实现
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.branch_map_2);

		ActionBar bar = getActionBar();
        bar.setHomeButtonEnabled(true);
        bar.setIcon(R.drawable.actionbar_icon);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setBackgroundDrawable(new ColorDrawable(0xff34a4f7));
        
		Intent parent = getIntent();
		String center = parent.getStringExtra("XY");
		String name = parent.getStringExtra("name");
		String address = parent.getStringExtra("address");

		setTitle(name);
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

		//定义Maker坐标点
		LatLng point = new LatLng(120.609502,31.310335);  
		//构建Marker图标  
		BitmapDescriptor bitmap = BitmapDescriptorFactory  
		    .fromResource(R.drawable.icon_marka);  
		//构建MarkerOption，用于在地图上添加Marker  
		OverlayOptions option = new MarkerOptions()  
		    .position(point)  
		    .icon(bitmap);  
		//在地图上添加Marker，并显示  
		mBaiduMap.addOverlay(option);
		
		mBaiduMap.setMyLocationEnabled(true);
		
//		MyLocationData.Builder locationDataBuilder = new MyLocationData.Builder();
//		locationDataBuilder.longitude(120.609502);
//		locationDataBuilder.latitude(31.310335);
//		mBaiduMap.setMyLocationData(locationDataBuilder.build());
//		
//		BitmapDescriptorFactory factory = new BitmapDescriptorFactory();
//		BitmapDescriptor bitmapDescriptor = factory.fromResource(R.drawable.icon_marka);
//		MyLocationConfigeration configeration = new MyLocationConfigeration(LocationMode.NORMAL,true,bitmapDescriptor);
//		mBaiduMap.setMyLocationConfigeration(configeration);
		
//		mPoiSearch = PoiSearch.newInstance();
//		
//		OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
//			public void onGetPoiResult(PoiResult result) {  
//			    if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {  
//			        return;  
//			    }  
//			    if (result.error == SearchResult.ERRORNO.NO_ERROR) {  
//			        mBaiduMap.clear();  
//			        //创建PoiOverlay  
//			        PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);  
//			        //设置overlay可以处理标注点击事件  
//			        mBaiduMap.setOnMarkerClickListener(overlay);  
//			        //设置PoiOverlay数据  
//			        overlay.setData(result);  
//			        //添加PoiOverlay到地图中  
//			        overlay.addToMap();  
//			        overlay.zoomToSpan();  
//			        return;  
//			    }  
//			} 
//
//			@Override
//			public void onGetPoiDetailResult(PoiDetailResult result) {
//				if (result.error != SearchResult.ERRORNO.NO_ERROR) {
//					Toast.makeText(MapDetailActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT)
//							.show();
//				} else {
//					Toast.makeText(MapDetailActivity.this, "成功，查看详情页面", Toast.LENGTH_SHORT)
//							.show();
//				}
//			}
//		};
//
//		mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
//		Log.v(TAG,"onCreate():start POI search");
//		mPoiSearch.searchInCity((new PoiCitySearchOption()).city("广州")
//				.keyword("华南师范大学").pageNum(2));
	}

	@Override
	protected void onDestroy() {
		// 在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
		mMapView.onDestroy();
//		mPoiSearch.destroy();
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
		mMapView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
		mMapView.onPause();
	}
	
	
	private class GetMapImageTask extends GetRequestTask {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			setProgressBarIndeterminateVisibility(true);
		}

		@Override
		protected void onPostExecute(InputStream result) {
			setProgressBarIndeterminateVisibility(false);
		}
	}

	private class MyPoiOverlay extends PoiOverlay {
		public MyPoiOverlay(BaiduMap baiduMap) {
			super(baiduMap);
		}

		@Override
		public boolean onPoiClick(int index) {
			super.onPoiClick(index);
			return true;
		}
	}

	
}
